package br.com.tcc.systemdx.persistence.hibernate;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import br.com.tcc.systemdx.domain.DescricaoDoenca;
import br.com.tcc.systemdx.domain.Sintoma;
import br.com.tcc.systemdx.persistence.DescricaoDoencaDAO;
import br.com.tcc.systemdx.persistence.SintomaDAO;

@Repository(DescricaoDoencaDAO.BEAN_NAME)
@Transactional
public class DescricaoDoencaHibernateDAOImpl extends
		AbstractEntityHibernateDAOImpl<DescricaoDoenca> implements
		DescricaoDoencaDAO {

	/**
	 * 
	 */
	private static final long serialVersionUID = -5097719524452097288L;
	
	@Autowired
	private SintomaDAO sintomaDAO;

	public List<DescricaoDoenca> findBySintomasIds(List<Long> sintomasIds) {
		List<DescricaoDoenca> doencas = new ArrayList<DescricaoDoenca>();
		for (Long sintomaId : sintomasIds) {
			DescricaoDoenca doenca = findBySintomaId(sintomaId);
			if (doenca != null) {
				doencas.add(doenca);
			}
		}
		return doencas;
	}

	public DescricaoDoenca findBySintomaId(Long sintomaId) {
		Sintoma sintoma = sintomaDAO.findById(sintomaId);
		if (sintoma != null)
			return sintoma.getDoenca();
		
		return null;
	}
	
	public List<DescricaoDoenca> findByAtributoIdValorId(Long atributoId, Long valorId) {
		Query query = _getSession().createQuery("select doenca from DescricaoDoenca as doenca join doenca.sintomas as sintoma " +
				"join sintoma.atributo as atributo join sintoma.valor as valor where atributo.id = ? and valor.id = ?");
		query.setLong(0, atributoId);
		query.setLong(1, valorId);
		List<DescricaoDoenca> listaDoencas = query.list();
		for (DescricaoDoenca doenca : listaDoencas) {
			doenca.getSintomas().size(); // Carrega os sintomas e os fatores relevantes
			doenca.getFatoresRelevantes().size();
		}
		return query.list();
	}



}
